﻿Public Class HabitatsGlob

    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Dim Niveau As Integer = 1
    Dim LibelleNiv As String()
    Dim CodeNiv As String()
    Dim NomNiv As String()
    Dim IdNiv As String()
    Dim Nomtable As String()
    Dim NbrNiv As Integer = 0
    Dim idAct As String = ""
    Public databaseName As String
    Private Sub HabitatsGlob_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ComboBoxIdTyp.Visible = False
        Me.ComboBoxIdRar.Visible = False
        Me.ComboBoxIdEnd.Visible = False
        Me.ComboBoxIdCatg.Visible = False


        db.myReq = "SELECT COUNT(syscolumns.name) AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'HabitatsTyp'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            NbrNiv = db.rdr(0) - 9
        End While
        db.Disconnecter()
        MsgBox("Intial" & NbrNiv + 9)
        LibelleNiv = New String(NbrNiv - 1) {}
        CodeNiv = New String(NbrNiv - 1) {}
        NomNiv = New String(NbrNiv - 1) {}
        IdNiv = New String(NbrNiv - 1) {}
        Nomtable = New String(NbrNiv + 9) {}
        Dim iter As Integer = 1
        While iter < NbrNiv
            db.myReq = "SELECT [Libelle_Niveau] FROM  [dbo].[Niveau] Where [Table_Niveau]='HabitatsTyp' and [Niv_Niveau]='" & iter & "'"
            db.Connecter(databaseName)
            While db.rdr.Read()
                LibelleNiv(iter - 1) = db.rdr(0)
            End While
            db.Disconnecter()
            iter = iter + 1
        End While

        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'HabitatsTyp'"
        db.Connecter(databaseName)
        iter = 0
        While db.rdr.Read()
            Nomtable(iter) = db.rdr(0)
            iter = iter + 1
        End While
        db.Disconnecter()

        Niveau = 1

        majEnd()
        majRar()
        majCatg()
        majTyp()


    End Sub

    Private Sub majRar()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[TypHabitRar]"
            db.Connecter(databaseName)
            Me.ComboBoxIdRar.Items.Clear()
            Me.ComboBoxNomRar.Items.Clear()
            Me.ComboBoxIdRar.Items.Add("")
            Me.ComboBoxNomRar.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxIdRar.Items.Add(db.rdr(0))
                Me.ComboBoxNomRar.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxIdRar.SelectedIndex = 0
            Me.ComboBoxNomRar.SelectedIndex = 0
            db.Disconnecter()

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub majEnd()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[TypHabitEnd]"
            db.Connecter(databaseName)
            Me.ComboBoxIdEnd.Items.Clear()
            Me.ComboBoxNomEnd.Items.Clear()
            Me.ComboBoxIdEnd.Items.Add("")
            Me.ComboBoxNomEnd.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxIdEnd.Items.Add(db.rdr(0))
                Me.ComboBoxNomEnd.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxIdEnd.SelectedIndex = 0
            Me.ComboBoxNomEnd.SelectedIndex = 0
            db.Disconnecter()

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub majCatg()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[HabTypols]"
            db.Connecter(databaseName)
            Me.ComboBoxIdCatg.Items.Clear()
            Me.ComboBoxCodeCatg.Items.Clear()
            Me.ComboBoxNomCatg.Items.Clear()
            Me.ComboBoxIdCatg.Items.Add("")
            Me.ComboBoxCodeCatg.Items.Add("")
            Me.ComboBoxNomCatg.Items.Add("Tous")
            While db.rdr.Read()
                Me.ComboBoxIdCatg.Items.Add(db.rdr(0))
                Me.ComboBoxCodeCatg.Items.Add(db.rdr(1))
                Me.ComboBoxNomCatg.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxIdCatg.SelectedIndex = 0
            Me.ComboBoxCodeCatg.SelectedIndex = 0
            Me.ComboBoxNomCatg.SelectedIndex = 0
            db.Disconnecter()

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub majTyp()
        Try
            Dim requeteX As String
            Dim niv As Integer = 1

            If Me.ComboBoxIdCatg.Text = "" Then
                requeteX = "Select * from  [dbo].[HabitatsTyp] WHERE " & Nomtable(6 + NbrNiv) & " = '" & Niveau & "'"
            Else
                requeteX = "Select * from  [dbo].[HabitatsTyp] WHERE " & Nomtable(6 + NbrNiv) & " = '" & Niveau & "' and " & Nomtable(2) & " = " & Me.ComboBoxIdCatg.Text & ""
            End If


            While niv < Niveau
                requeteX = requeteX & "and " & Nomtable(5 + niv) & " = '" & CodeNiv(niv - 1) & "' "
                niv = niv + 1
            End While


            If Niveau = 1 Then
                Me.ButtonBack.Enabled = False
                Me.ButtonNext.Enabled = True
            ElseIf Niveau = NbrNiv Then
                Me.ButtonBack.Enabled = True
                Me.ButtonNext.Enabled = False
            Else
                Me.ButtonBack.Enabled = True
                Me.ButtonNext.Enabled = True
            End If

            db.myReq = requeteX

            db.Connecter(databaseName)

            Me.ComboBoxIdTyp.Items.Clear()
            Me.ComboBoxNomTyp.Items.Clear()
            While db.rdr.Read()
                Me.ComboBoxNomTyp.Items.Add(db.rdr(NbrNiv + 7))
                Me.ComboBoxIdTyp.Items.Add(db.rdr(0))
            End While
            db.Disconnecter()

            If ComboBoxIdTyp.Items.Count < 0 Or Niveau = NbrNiv Then
                Me.ButtonNext.Enabled = False
            Else
                Me.ButtonNext.Enabled = True
            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub


    Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
        Entrer = ""
        Niveau = Niveau + 1
        Me.LabelNiv.Text = LibelleNiv(Niveau)

        db.myReq = "Select * from  [dbo].[HabitatsTyp] WHERE " & Nomtable(0) & " = '" & Me.ComboBoxIdTyp.Text & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            CodeNiv(Niveau - 2) = db.rdr(4 + Niveau)
        End While

        majTyp()
    End Sub
    Private Sub ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBack.Click
        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        Entrer = ""
        Niveau = Niveau - 1
        Me.LabelNiv.Text = LibelleNiv(Niveau - 1)
        While niv < Niveau
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While
        majTyp()
        Me.ComboBoxNomTyp.SelectedIndex = Me.ComboBoxIdTyp.Items.IndexOf(IdNiv(Niveau - 1))
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouvTypol.Click
        Dim typX As New HABITATSTYP
        typX.Text = "Typologie"
        typX.Visible = True
    End Sub


    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Dim idHabG As Integer

        db.myReq = " INSERT INTO  [dbo].[HABITATSGLO]([ID_Sit],[DOMHAB_HABG],[EXPLHAB_HABG]) VALUES('" & db.idSite & "','" & Me.TextBoxDom.Text & "','" & Me.TextBoxDesc.Text & "')"
        db.Connecter(databaseName)
        db.Disconnecter()

        db.myReq = "Select max([Id_HabG]) FROM  [dbo].[HABITATSGLO]"
        db.Connecter(databaseName)
        While db.rdr.Read()
            idHabG = db.rdr(0)
        End While
        db.Disconnecter()

        db.myReq = " INSERT INTO  [dbo].[HabitatsDet]([Id_HabG],[Id_Hab],[Loc_HabD],[Expl_HabD]) VALUES('" & idHabG & "','" & Me.ComboBoxIdTyp.Text & "','" & Me.TextBoxLoc.Text & "','" & Me.TextBoxDescDet.Text & "')"
        db.Connecter(databaseName)
        db.Disconnecter()
    End Sub

    Private Sub ComboBoxNomCatg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomCatg.SelectedIndexChanged
        Try
            If Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text) > 0 Then
                Dim Pos As Int32 = Me.ComboBoxNomCatg.Text.Length
                Me.ComboBoxNomCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
                Me.ComboBoxNomCatg.SelectionStart = Pos
                Me.ComboBoxNomCatg.SelectionLength = Me.ComboBoxNomCatg.Text.Length - Pos
                Me.ComboBoxIdCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
                Me.ComboBoxCodeCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
            Else
                Me.ComboBoxIdCatg.Text = ""
                Me.ComboBoxCodeCatg.Text = ""
            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomCatg_Clik(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomCatg.MouseDoubleClick
        majCatg()
    End Sub
    Private Sub ComboBoxNomTyp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomTyp.SelectedIndexChanged
        Try
            Dim niv As Integer = 0
            Dim IdRar, IdEnd, idTyp As Integer
            Me.TextBoxDecoupageHab.Text = ""

            Dim Pos As Int32 = Me.ComboBoxNomTyp.Text.Length
            Me.ComboBoxNomTyp.SelectedIndex = Me.ComboBoxNomTyp.FindString(Me.ComboBoxNomTyp.Text)
            Me.ComboBoxNomTyp.SelectionStart = Pos
            Me.ComboBoxNomTyp.SelectionLength = Me.ComboBoxNomTyp.Text.Length - Pos
            Me.ComboBoxIdTyp.SelectedIndex = Me.ComboBoxNomTyp.FindString(Me.ComboBoxNomTyp.Text)


            db.myReq = "Select * from  [dbo].[HABITATSTYP] WHERE " & Nomtable(0) & " = '" & Me.ComboBoxIdTyp.Text & "'"
            db.Connecter(databaseName)
            While db.rdr.Read
                IdRar = db.rdr(3)
                IdEnd = db.rdr(1)
                idTyp = db.rdr(2)
                NomNiv(Niveau - 1) = db.rdr(12)
            End While
            db.Disconnecter()
            Me.ComboBoxNomRar.SelectedIndex = Me.ComboBoxIdRar.FindString(IdRar)
            Me.ComboBoxNomEnd.SelectedIndex = Me.ComboBoxIdEnd.FindString(IdEnd)
            Me.ComboBoxNomCatg.SelectedIndex = Me.ComboBoxIdCatg.FindString(idTyp)
            Me.ComboBoxCodeCatg.SelectedIndex = Me.ComboBoxIdCatg.FindString(idTyp)
            Me.ComboBoxIdCatg.SelectedIndex = Me.ComboBoxIdCatg.FindString(idTyp)


            While niv < NbrNiv
                Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
                niv = niv + 1
            End While

            IdNiv(Niveau - 1) = Me.ComboBoxNomTyp.SelectedIndex
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomTyp_Clik(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomTyp.MouseDoubleClick
        majTyp()
    End Sub
    Private Sub ComboBoxNomRar_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomRar.SelectedIndexChanged
        Try
            If Me.ComboBoxNomRar.FindString(Me.ComboBoxNomRar.Text) > 0 Then
                Dim Pos As Int32 = Me.ComboBoxNomRar.Text.Length
                Me.ComboBoxNomRar.SelectedIndex = Me.ComboBoxNomRar.FindString(Me.ComboBoxNomRar.Text)
                Me.ComboBoxNomRar.SelectionStart = Pos
                Me.ComboBoxNomRar.SelectionLength = Me.ComboBoxNomRar.Text.Length - Pos
                Me.ComboBoxIdRar.SelectedIndex = Me.ComboBoxNomRar.FindString(Me.ComboBoxNomRar.Text)
                Me.ComboBoxCodeCatg.SelectedIndex = Me.ComboBoxNomRar.FindString(Me.ComboBoxNomRar.Text)
            Else
                Me.ComboBoxIdRar.Text = ""
                Me.ComboBoxCodeCatg.Text = ""
            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomRar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomRar.MouseDoubleClick
        majRar()
    End Sub

    Private Sub ComboBoxNomEnd_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomEnd.SelectedIndexChanged
        Try
            If Me.ComboBoxNomEnd.FindString(Me.ComboBoxNomEnd.Text) > 0 Then
                Dim Pos As Int32 = Me.ComboBoxNomEnd.Text.Length
                Me.ComboBoxNomEnd.SelectedIndex = Me.ComboBoxNomEnd.FindString(Me.ComboBoxNomEnd.Text)
                Me.ComboBoxNomEnd.SelectionStart = Pos
                Me.ComboBoxNomEnd.SelectionLength = Me.ComboBoxNomEnd.Text.Length - Pos
                Me.ComboBoxIdEnd.SelectedIndex = Me.ComboBoxNomEnd.FindString(Me.ComboBoxNomEnd.Text)
                Me.ComboBoxCodeCatg.SelectedIndex = Me.ComboBoxNomEnd.FindString(Me.ComboBoxNomEnd.Text)
            Else
                Me.ComboBoxIdEnd.Text = ""
                Me.ComboBoxCodeCatg.Text = ""
            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomEnd.MouseDoubleClick
        majEnd()
    End Sub

    Private Sub ButtonNouvCatg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouvCatg.Click
        Dim typ1 As New TypologieNiv1Form(databaseName)
        typ1.Text = "Catégorie"
        typ1.Table = "HabTypols"
        typ1.Visible = True
    End Sub

    Private Sub ButtonNouvRar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouvRar.Click
        Dim typ1 As New TypologieNiv1Form(databaseName)
        typ1.Text = "Catégorie"
        typ1.Table = "TypHabitRar"
        typ1.Visible = True
    End Sub

    Private Sub ButtonNouvEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouvEnd.Click
        Dim typ1 As New TypologieNiv1Form(databaseName)
        typ1.Text = "Catégorie"
        typ1.Table = "TypHabitEnd"
        typ1.Visible = True
    End Sub


    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

    End Sub
End Class